package version1

import (
	"server/global"
	operatemodel "server/model/operate"
	"server/myutils"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/shirou/gopsutil/v3/disk"
	"github.com/shirou/gopsutil/v3/host"
	"github.com/shirou/gopsutil/v3/mem"
)

//@Summary 仪表盘信息获取
//@Router /v1/info [get]
//@Tags Gets
//@Description 获取仪表盘数据 包括:今日天气情况,在册用户,今日访问量
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func Info(req *gin.Context) {
	//获取天气数据
	data, code := operatemodel.GetWeather()
	if code == global.SUCCESS {
		//获取在册用户
		i := operatemodel.GetuserNum()
		if i != -1 {
			//获取流量(中间件规划)
			i2, err := operatemodel.Getflow()
			if err == nil {
				a := operatemodel.Fetchnewblog()
				if a != nil {
					myutils.ResultJSON(req, code, gin.H{
						"weather": data,
						"usernum": i,
						"flow":    i2,
						"newblog": a,
					})
				}

				return
			}
		}
	}
	myutils.ResultJSON(req, global.FAIL, nil)
}

//@Summary 获取当前服务器的信息
//@Router /v1/hoststatus [get]
//@Tags Gets
//@Description  获取的信息有:cpu信息;内存信息;系统信息
//@Produce json
//@Accept json
//@Success 200 {object} model.StandardResponse{} `接口被正确调用,但是调用结果看status状态码`
func Gethostinfo(req *gin.Context) {
	//获得内存信息
	vms, _ := mem.VirtualMemory()
	//获取物理cpu核心数
	physicalcpu, _ := cpu.Counts(false)
	//使用率(会阻塞获取时间内的运行使用率[因为要求平均值])
	cpupercent, _ := cpu.Percent(time.Second, false)
	//cpu详细信息
	cpuinfo, _ := cpu.Info()
	//系统内核及其版本
	platform, hostfamily, _, _ := host.PlatformInformation()
	//开机时间
	openhost, _ := host.BootTime()
	//磁盘分区情况
	us, _ := disk.Usage("D:/")

	var maper = map[string]interface{}{
		"memory": map[string]interface{}{
			"allmemory":   vms.Total / 1024 / 1024,
			"usedmemory":  vms.Used / 1024 / 1024,
			"availmemory": vms.Available / 1024 / 1024,
			"percent":     vms.UsedPercent,
		},
		"cpu": map[string]interface{}{
			"cpucount":   physicalcpu,
			"usepercent": cpupercent,
			"info":       cpuinfo,
		},
		"host": map[string]interface{}{
			"platform":   platform,
			"hostfamily": hostfamily,
			"openhost":   openhost,
		},
		"disk": us,
	}

	myutils.ResultJSON(req, global.SUCCESS, maper)
	
}
